<?php
/**
 * Created by PhpStorm.
 * User: zhangyi
 * Date: 17/3/10
 * Time: 下午10:23
 */
namespace backend\controllers;
use app\models\CaptchaLog;
use app\models\HeaderInfo;
use app\models\Member;
use app\models\SmsLog;
use yii\helpers\Url;
use Yii;

class AuthController extends BaseInterfaceController
{
    public function actions() {
        return [
            'captcha' =>  [
                'class' => 'backend\components\CustomAction',
                'height' => 50,
                'width' => 80,
                'minLength' => 4,
                'maxLength' => 4
            ],
        ];
    }

    //发送验证码
    public function actionVerifycode()
    {
        $mobile = $this->originData['mobile'];
        $action = $this->originData['action'];
        $code = $this->originData['code'];
        if($this->verifyMobileValid($mobile)) {
            $captchaModel = CaptchaLog::findOne(['mobile'=>$mobile,'action'=>$action]);
            if($captchaModel && $captchaModel->code==$code) {
                if($this->checkSmsSendLimit($mobile)) {
                    $mcode = rand(1000,9999);
                    $message = '您正在进行'.SmsLog::$actionArray[$action].'操作,验证码为:'.$mcode;
                    $model = new SmsLog();
                    $model->mobile = $mobile;
                    $model->code = strval($mcode);
                    $model->action = $action;
                    $model->ip = $_SERVER["REMOTE_ADDR"];

                    if($model->save()) {
                        $this->sms_send($mobile,$message);
                    } else {
                        $this->status=-1;
                        $this->message='网站内部错误,请联系管理员';
		    }
                } else {
                    $this->status=-1;
                    $this->message='发送短信已超过限制';
                }
            } else {
                $this->status=-1;
                $this->message='验证码错误';
            }

        } else {
            $this->status=-1;
            $this->message='手机号码格式错误';
        }
        return $this->outputJson();
    }
    //注册第一步
    public function actionRegisterStepone()
    {
        $mobile = $this->originData['mobile'];
        $verify_code = $this->originData['verify_code'];
        if($this->verifyMobileValid($mobile)) {
            $model = SmsLog::find()->where(['mobile'=>$mobile])->orderBy('id desc')->one();
            if($model) {
                if($model->code != $verify_code) {
                    $this->status=-1;
                    $this->message='验证码错误';
                }
            } else {
                $this->status=-1;
                $this->message='没有找到短信记录';
            }

        } else {
            $this->status=-1;
            $this->message='手机号码格式错误';
        }
        return $this->outputJson();
    }
    //注册第二步
    public function actionRegisterSteptwo()
    {
        $mobile = $this->originData['mobile'];
        $password = $this->originData['password'];
        $nickname = $this->originData['nickname'];
        $channel = $this->originData['channel'];
        $product = $this->originData['product'];

        if($this->verifyMobileValid($mobile)) {
            $have = Member::find()->where(['mobile'=>$mobile])->one();
            if(empty($have)) {
                $this->status=-1;
                $this->message='手机号不存在';

            } elseif(empty($have->password)) {
           	 $model = $have;
           	 $model->generatePassword($password);
           	 $model->username = $nickname;
           	 $model->channel = $channel;
           	 $model->product = $product;

           	 if(!$model->save()) {
           	     $this->status=-1;
           	     $this->message='创建用户失败';
           	 } else {
                 $this->logHeaderInfo($model->id,HeaderInfo::TYPE_REGISTER);
             }
           } else {
                    $this->status=-1;
                    $this->message='手机号已经注册';
           }
        } else {
            $this->status=-1;
            $this->message='手机号码格式错误';
        }
        return $this->outputJson();
    }
    //重置密码一
    public function actionPasswordResetone()
    {
        $mobile = $this->originData['mobile'];
        $verify_code = $this->originData['verify_code'];

        if($this->verifyMobileValid($mobile)) {
            $smsLog = SmsLog::find()->where(['mobile'=>$mobile])->orderBy('id desc')->one();
            if($smsLog->code != $verify_code) {
                $this->status=-1;
                $this->message='验证码错误';
            }

        } else {
            $this->status=-1;
            $this->message='手机号码格式错误';
        }
        return $this->outputJson();
    }
    //重置密码二
    public function actionPasswordResettwo()
    {
        $mobile = $this->originData['mobile'];
        $password = $this->originData['password'];

        if($this->verifyMobileValid($mobile)) {
            $model = Member::findOne(['mobile'=>$mobile]);
            if(empty($model)) {
                $this->status=-1;
                $this->message='尚未注册的手机用户';
            } else {
                $model->generatePassword($password);
                if(!$model->save()) {
                    $this->status=-1;
                    $this->message='重置密码失败';
                }
            }

        } else {
            $this->status=-1;
            $this->message='手机号码格式错误';
        }
        return $this->outputJson();
    }
    //修改密码
    public function actionPasswordModify()
    {
        $uid = $this->originData['uid'];
        $old_password = $this->originData['old_password'];
        $new_password = $this->originData['new_password'];
        $model = Member::findOne($uid);
        if($model) {
            if($model->password == $model->encry($old_password)) {
                $model->generatePassword($new_password);
                if(!$model->save()) {
                    $this->status=-1;
                    $this->message='操作失败';
                }
            } else {
                $this->status=-1;
                $this->message='旧密码错误';
            }

        } else {
            $this->status=-1;
            $this->message='用户不存在';
        }


        return $this->outputJson();
    }
    //登录
    public function actionLogin()
    {
        $mobile = $this->originData['mobile'];
        $password = $this->originData['password'];

        if($this->verifyMobileValid($mobile)) {
            $model = Member::find()->where(['mobile'=>$mobile])->one();
            if(empty($model)) {
                $this->status=-1;
                $this->message='用户不存在,请注册';
	    } elseif ($model->status==Member::STATUS_DENYLOGIN) {
                $this->status=-1;
                $this->message='用户已经禁止登录';
            } elseif ($model->password != $model->encry($password)) {
                $this->status=-1;
                $this->message='密码错误';
            } else {
                $this->content = array(
                    'uid'=>$model->id,
                    'nickname'=>$model->username,
                );
                $this->logHeaderInfo($model->id,HeaderInfo::TYPE_LOGIN);
            }

        } else {
            $this->status=-1;
            $this->message='手机号码格式错误';
        }
        return $this->outputJson();
    }

}
